package com.liuwang.meal.manager;

//菜谱管理类
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liuwang.meal.bean.Menu;
import com.liuwang.meal.db.DBHelper;
import com.liuwang.meal.db.Menus;

public class MenuManager {
	private Context context = null;

	public MenuManager(Context context) {
		this.context = context;
	}
	
	// 按照类型查询所有菜谱
	public List<Menu> queryMenus(int type_id){
		// 获取数据库
		DBHelper db = new DBHelper(this.context);
		SQLiteDatabase database = db.getReadableDatabase();
		String sql = "select _id,typeId,name,description,price,unit,pic,remark from "+Menus.TABLE+" where typeID=?";
     	// 执行语句
    	Cursor  cursor = database.rawQuery(sql,new String[]{type_id+""});
    	// 循环遍历结果集
    	List<Menu>  list = new ArrayList<Menu>();

    	while(cursor.moveToNext()){
    		Integer _id = cursor.getInt(cursor.getColumnIndex("_id"));
    		Integer typeId = cursor.getInt(cursor.getColumnIndex("typeId"));
    		String name = cursor.getString(cursor.getColumnIndex("name"));
    		String description = cursor.getString(cursor.getColumnIndex("description"));
    		Integer price = cursor.getInt(cursor.getColumnIndex("price"));
    		String unit = cursor.getString(cursor.getColumnIndex("unit"));
    		String pic = cursor.getString(cursor.getColumnIndex("pic"));
    		String remark = cursor.getString(cursor.getColumnIndex("remark"));
    		Menu user = new Menu(_id,typeId,name,description,price,unit,pic,remark);
    	    list.add(user);
    	}

    	// 关闭数据库
    	database.close();
    	return list;
	}
	// 根据ID查询一个菜谱
	public Menu query(int id){
		// 获取数据库
		DBHelper db = new DBHelper(this.context);
		SQLiteDatabase database = db.getReadableDatabase();
		String sql = "select _id,typeId,name,description,price,unit,pic,remark from "+Menus.TABLE+" where _id=?";
     	// 执行语句
    	Cursor  cursor = database.rawQuery(sql,new String[]{id+""});
    	// 循环遍历结果集
    	Menu  menu =null;
    	if(cursor.moveToFirst()){
    		Integer _id = cursor.getInt(cursor.getColumnIndex("_id"));
    		Integer typeId = cursor.getInt(cursor.getColumnIndex("typeId"));
    		String name = cursor.getString(cursor.getColumnIndex("name"));
    		String description = cursor.getString(cursor.getColumnIndex("description"));
    		Integer price = cursor.getInt(cursor.getColumnIndex("price"));
    		String unit = cursor.getString(cursor.getColumnIndex("unit"));
    		String pic = cursor.getString(cursor.getColumnIndex("pic"));
    		String remark = cursor.getString(cursor.getColumnIndex("remark"));
    		menu = new Menu(_id,typeId,name,description,price,unit,pic,remark);
    	}

    	// 关闭数据库
    	database.close();
    	return menu;
	}
}
